#!/usr/bin/python3
# coding=utf-8

from bs4 import BeautifulSoup
import sys, html5lib, time, random
from DB.MysqlDB import *
from NOVEL.MODEl.UnifyBook import *
from NOVEL.MODEl.UnifyBookCapter import *
from REQUEST.RequestUtil import *
from NOVEL.MODEl.UnifyBookCatalog import *
from NOVEL.BQK.Novel import *
from  NOVEL.BQK.NovelThread import *
import operator as op
import re, time, threading

class BQKWebsite:

    def __init__(self) -> None:
        super().__init__()
        self.indexUrl = 'https://www.biqukan.com'
        # self.DB = MysqlDB()
        self.request = RequestUtil()
        self.categoryUrlArray = []
        self.categoryNameArray = []
        # self.recentlyChapter = None # 最近下载的章节标题

    '''
    首页解析,将分类链接拼接到成员变量
    '''
    def analysisIndex(self):
        html = self.request.getHtml(self.indexUrl).text
        categories = BeautifulSoup(html, 'html5lib').find_all('div', class_='nav')[0].find_all('a')
        # categories = BeautifulSoup(navText, 'html5lib').find_all('li')
        for category in categories[8:9]:
            categoryName = category.text
            refUrl = self.indexUrl + category.get('href')
            print(categoryName, refUrl)
            self.categoryUrlArray.append(refUrl)
            self.categoryNameArray.append(categoryName)
        pass

    '''
    具体小说目录链接解析
    '''
    def dealCategorys(self):
        for i in range(len(self.categoryUrlArray)):
            # 处理小说目录列表 l bd
            categoryUrl = self.categoryUrlArray[i]
            categoryName = self.categoryNameArray[i]
            print('.....................................', categoryUrl, categoryName)
            # NovelThread(threadID = i, categoryUrl = categoryUrl, categoryName=categoryName, indexUrl = self.indexUrl).start()
            self.analysisCatalog(refUrl=categoryUrl)
    pass

    '''
       解析目录(由线程执行 )
       '''
    def analysisCatalog(self, refUrl):
        html = self.request.getHtml(refUrl).text
        novels = BeautifulSoup(html, 'html5lib').find_all('div', class_='l bd')[0].find_all('span', class_='s2')
        for novelInfo in novels:
            novelTitle = novelInfo.text
            novelRef = self.indexUrl + novelInfo.a.get('href')
            print(novelTitle, novelRef)
            novel = Novel(indexUrl=self.indexUrl, catalogUrl=novelRef)
            novel.copyNovel()

    '''
    解析网站
    '''
    def analysisWebsite(self):
        self.analysisIndex()
        self.dealCategorys()
        # self.analysisCatalog('https://www.biqukan.com/dushixiaoshuo/')


if __name__ == '__main__':
    bqk = BQKWebsite()
    bqk.analysisWebsite()
